package cn.tedu.csmall.product;

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.ValueOperations;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Slf4j
@SpringBootTest
public class RedisTetes {

    @Autowired
    private RedisTemplate<String, Serializable> redisTemplate;

    @Test
    void valueSet(){
        String key = "username2";
        String value = "bulaike1";

        ValueOperations<String,Serializable> ops = redisTemplate.opsForValue();
        ops.set(key,value);
    }
    @Test
    void valueGet(){
        String key ="username1";

        ValueOperations<String,Serializable> ops = redisTemplate.opsForValue();
        Serializable serializable = ops.get(key);
        log.debug("{}",serializable);
    }
    @Test
    void keys(){
        String pattern = "username*";
        Set<String> keys = redisTemplate.keys(pattern);
        log.debug("根据模式【{}】搜索Key，结果：{}", pattern, keys);
    }

    @Test
    void delete() {
        String key = "username1";

        Boolean result = redisTemplate.delete(key);
        log.debug("根据Key【{}】删除数据完成，结果：{}", key, result);
    }

    @Test
    void deleteX() {
        Set<String> keys = new HashSet<>();
        keys.add("username1");
        keys.add("username2");
        keys.add("username3");

        Long count = redisTemplate.delete(keys);
        log.debug("根据Key集合【{}】删除数据完成，成功删除的数据的数量：{}", keys, count);
    }

    @Test
    void list(){
        String key = "stringList";
        List<String> stringList = new ArrayList<>();
        for (int i = 1; i <= 10; i++) {
            stringList.add("string-" + i);
        }

        ListOperations<String, Serializable> ops = redisTemplate.opsForList();
        for (String s : stringList) {
            ops.rightPush(key, s);
        }
    }
    @Test
    void size(){
        String key = "stringList";
        ListOperations<String,Serializable> ops = redisTemplate.opsForList();
        Long size = ops.size(key);
        log.debug("根据key{}读取列表的长度,结果:{}",key,size);
    }

    @Test
    void     range(){
        String key = "stringList";
        long start = 0;
        long end = -1;

        ListOperations<String, Serializable> ops = redisTemplate.opsForList();
        List<Serializable> list = ops.range(key, start, end);
        log.debug("根据key{},从{}到{}读取列表,结果长度:{}",key,start,end,list.size());
        for (Serializable item : list) {
            log.debug("列表项:{}",item);
        }
    }

    @Test
    void add(){
        SetOperations<String, Serializable> ops = redisTemplate.opsForSet();
        String key  = "brandKeys";
        String value = "brand:item:2";
        ops.add(key,value);
    }

    @Test
    void members(){
        SetOperations<String, Serializable> ops = redisTemplate.opsForSet();
        String key ="brandKeys";
        Set<Serializable> members = ops.members(key);
        log.debug("{}",members);
    }
}
